package org.tang.schoolproject.service.Imp;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tang.schoolproject.entitys.LoginUser;
import org.tang.schoolproject.entitys.User;
import org.tang.schoolproject.mapper.UserMapper;
import org.tang.schoolproject.entitys.properties.SaTokenProperties;
import org.tang.schoolproject.service.UserService;

import java.util.Map;

@Service
@Transactional
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final UserMapper userMapper;
    private final SaTokenProperties saTokenProperties;

    /**
     * 登录接口
     *
     * @param user
     * @return
     */
    @Override
    public SaResult login(LoginUser user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 == null) return SaResult.error("用户不存在");

        String password = SaSecureUtil.aesDecrypt(saTokenProperties.getVerifyCode(), user1.getPassword());

        if (user1.getStatus() != 0) return SaResult.error("用户被禁用");
        if (user1.getPassword().equals(user.getPassword())){
            StpUtil.login(user1.getPhone());
            return SaResult.data(StpUtil.getTokenInfo());
        }
        return SaResult.error("密码错误");
    }

    @Override
    public SaResult saveUser(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, user.getPhone());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 != null) return SaResult.error("用户已存在");
        String cipher = SaSecureUtil.aesEncrypt(saTokenProperties.getVerifyCode(), user.getPassword());
        user.setPassword(cipher);
        return userMapper.insert(user) > 0 ? SaResult.ok("注册成功") : SaResult.error("注册失败");
    }

    @Override
    public SaResult selectUser(Long id) {
        // 1.查询信息
        User user = userMapper.selectById(id);
        //判断用户是否存在
        if (user == null) return SaResult.error("用户不存在");
        //隐藏密码
        user.setPassword( null);
        return SaResult.data(user);
   }

   @Override
    public SaResult updateUser(User user) {
        User user1 = userMapper.selectById(user.getId());
        if (user1 == null){
            return SaResult.error("用户不存在");
        }else if (user.getPassword() != null && !user.getPassword().isEmpty()){
            String pas = SaSecureUtil.aesEncrypt(saTokenProperties.getVerifyCode(), user.getPassword());
            user.setPassword(pas);
        }else if (user.getPhone() != null && !user.getPhone().isEmpty()){
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, user.getPhone());
            User user2 = userMapper.selectOne(wrapper);
            if (user2 != null) return SaResult.error("此电话已被注册");
        }
        return userMapper.updateById(user) > 0 ? SaResult.ok("修改成功") : SaResult.error("修改失败");
    }

    @Override
    public SaResult deleteUser(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) return SaResult.error("用户不存在");
        return userMapper.deleteById(id) > 0 ? SaResult.ok("删除成功") : SaResult.error("删除失败");
    }

    @Override
    public SaResult pageUser(Integer pageNum, Integer pageSize, Map<String, Object> searchMap) {
        Page< User> userPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        //查询提交时间在某个范围之间的用户
        if (searchMap.get("startTime") != null && !searchMap.get("startTime").toString().isEmpty()){
            wrapper.ge(User::getSubmitTime, searchMap.get("startTime"));
        }

        if (searchMap.get("endTime") != null && !searchMap.get("endTime").toString().isEmpty()){
            wrapper.le(User::getSubmitTime, searchMap.get("endTime"));
        }
        Page<User> page = userMapper.selectPage(userPage, wrapper);
        return SaResult.data(page);
    }
}
